package com.message.tcp.ip.ui.account.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.message.tcp.ip.socket.configure.entity.CardAccount;
import com.message.tcp.ip.socket.configure.service.CardAccountService;
import com.message.tcp.ip.ui.account.panel.CardAccountPanel;
import com.message.tcp.ip.ui.member.dialog.AccountTable;

public class CardAccountAction implements ActionListener {
	private JComboBox<String> binBox;
	private JTextField cardNumber;
	private JComboBox<String> amttBox;
	private JComboBox<String> currBox;
	private JComboBox<String> flagBox;
	private JTextField balance;
	private JTextField expiryDate;
	private JTextField cav;
	private AccountTable table;
	private CardAccountPanel panel;
	private CardAccountService service;
	public CardAccountAction(JComboBox<String> binBox, JTextField cardNumber,
			JComboBox<String> amttBox, JComboBox<String> currBox,
			JComboBox<String> flagBox, JTextField balance,
			JTextField expiryDate, JTextField cav, AccountTable table,
			CardAccountPanel panel) {
		this.binBox = binBox;
		this.cardNumber = cardNumber;
		this.amttBox = amttBox;
		this.currBox = currBox;
		this.flagBox = flagBox;
		this.balance = balance;
		this.expiryDate = expiryDate;
		this.cav = cav;
		this.table = table;
		this.panel = panel;
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		service = (CardAccountService) context.getBean("cardAccountService");
	}
	@Override
	public void actionPerformed(ActionEvent event) {
		String bin = (String) binBox.getSelectedItem();
		String card = cardNumber.getText();
		String amtType = (String) amttBox.getSelectedItem();
		String curr = (String) currBox.getSelectedItem();
		String flag = (String) flagBox.getSelectedItem();
		double bal = Double.parseDouble(balance.getText());
		String exp = expiryDate.getText();
		String cv = cav.getText();
		
		CardAccount acct = new CardAccount();
		acct.setBin(bin);
		acct.setCard(card);
		acct.setAmtType(amtType);
		acct.setCurr(curr);
		acct.setFlag(flag);
		acct.setBal(bal);
		acct.setExpDate(exp);
		acct.setCav(cv);
		CardAccount ac = service.getCardAccount(acct);
		if(ac == null){
			service.save(acct);
			JOptionPane.showMessageDialog(null, "Success !");
			panel.dispose();
			CardAccount cardAccounts = service.getCardAccount(acct);
			DefaultTableModel model = (DefaultTableModel) table.getModel();
			model.addRow(new Object[] { cardAccounts.getId(),
					cardAccounts.getCard(), cardAccounts.getAmtType(),
					cardAccounts.getCurr(), cardAccounts.getFlag(),
					cardAccounts.getBal(), cardAccounts.getExpDate(),
					cardAccounts.getCav(), cardAccounts.getBin() });
			table.setModel(model);
		}
		else{
			JOptionPane.showMessageDialog(null, "Duplicate CARD", "Error Dialog", JOptionPane.ERROR_MESSAGE);
		}
	}

}
